package com.xjj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjj.entity.*;
import com.xjj.excel.ExcelUtils;
import com.xjj.service.SchoolService;
import com.xjj.service.StudentService;
import com.xjj.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author sxy
 * @version 1.0
 * @date 2022/10/20 14:13
 */
@RestController
@RequestMapping("/manage/admin/student")
@Api(value = "该类是学生的相关模块")
public class StudentController {
    @Resource
    private StudentService studentService;

    @PostMapping("/addStudent")
    @ApiOperation(value = "添加学生")
    public ResponseEntity addStudent(@RequestBody Student student) {
        boolean save = studentService.save(student);
        return new ResponseEntity<>(200, "ok", save);
    }

    @DeleteMapping("/deleteStudentById/{id}")
    @ApiOperation(value = "通过id删除学生")
    public ResponseEntity deleteStudentById(@PathVariable int id) {
        boolean flag = studentService.removeById(id);
        return new ResponseEntity<>(200, "ok", flag);
    }

    @ApiOperation(value = "查找所有学生所有信息")
    @GetMapping("/findAllStudent")
    public ResponseEntity findAllStudent() {
        List<Student> students = studentService.findAllStudent();
        return new ResponseEntity<>(200, "ok", students);
    }

    @PostMapping("/import")
    @ApiOperation(value = "excel导入学生信息")
    public ResponseEntity importSchool(@RequestPart("file") MultipartFile file) throws Exception {
        List<Student> students = ExcelUtils.readMultipartFile(file, Student.class);
        for (Student student : students) {
            //          解决数字过大自动转化为科学计数法问题
            BigDecimal bigDecimal = new BigDecimal(student.getTel());//创建BigDecimal对象，把科学计数转成数字
            BigDecimal bigDecimal1 = new BigDecimal(student.getSid());//创建BigDecimal对象，把科学计数转成数字
            student.setTel(bigDecimal.toPlainString());
            student.setSid(bigDecimal1.toPlainString());
            studentService.save(student);
        }
        return ResponseEntity.SUCCESS("导入成功", 200);
    }

    @GetMapping("/getAllStudent/{current}")
    @ApiOperation(value = "分页查询所有学生")
    public ResponseEntity getAllStudentPage(@PathVariable("current") Integer current) {
        if (current == 0) {
            Page<Student> student = studentService.getStudent(1);
            return new ResponseEntity(200, "获取分页查询学校信息", student);
        } else {
            Page<Student> student = studentService.getStudent(current);
            return new ResponseEntity(200, "获取分页查询学校信息", student);
        }
    }

    @ApiOperation(value = "导出所有学生所有信息")
    @GetMapping("/export")
    public void exportStudent(HttpServletResponse response) {
        List<Student> students = studentService.list();
        ExcelUtils.export(response, "学生表", students, Student.class);
    }

    @PutMapping("/updateStudentById")
    @ApiOperation(value = "通过id修改学生信息")
    public ResponseEntity updateStudentById(@RequestBody Student student) {
        studentService.updateStudentById(student);
        return ResponseEntity.SUCCESS("ok", 200);
    }

    /**
     * <p>动态更新学生表信息</p>
     * <p>注意：学生 id 不可为空，否则将会导致无效参数异常</p>
     *
     * @param student 学生
     * @return 操作成功响应
     */
    @PostMapping("update")
    public ResponseEntity update(@RequestBody Student student) {
        studentService.updateByCondition(student);
        return ResponseEntity.SUCCESS("修改成功", studentService.getById(student.getId()));
    }

    @PostMapping("/list")
    public ResponseEntity<PageBean<Student>> list(@RequestBody StudentVo studentVo) {
        // 每页展示条数，暂定 5
        Integer pageSize = 10;

        Student student = studentVo.getStudent();
        Integer pageNum = studentVo.getPageNum() == null ? 1 : studentVo.getPageNum();
        PageBean<Student> pageBean = studentService.getByPage(student, pageNum, pageSize);
        return ResponseEntity.SUCCESS("获取列表成功", pageBean);
    }

    @GetMapping("/getStudentById/{id}")
    @ApiOperation(value = "根据学生id查询学生信息")
    public ResponseEntity getStudentById(@PathVariable int id) {
        Student student = studentService.getById(id);
        return new ResponseEntity(200, "ok", student);
    }

    @GetMapping("/getAllStudentsByKlassId/{klassId}")
    @ApiOperation(value = "根据班级id查询该班级所有学生信息")
    public ResponseEntity getAllStudentsByKlassId(@PathVariable int klassId) {
        List<Student> students = studentService.getAllStudentsByKlassId(klassId);
        return new ResponseEntity(200, "ok", students);
    }
    @GetMapping("/addSubmitNumById/{id}")
    @ApiOperation(value = "根据学生id增加提交作业的数量")
    public ResponseEntity addSubmitNumById(@PathVariable int id) {
        studentService.addSubmitNumById(id);
        return new ResponseEntity(200, "ok", "ok");
    }

    @GetMapping("/decreaseSubmitNumById/{id}")
    @ApiOperation(value = "根据学生id减少提交作业的数量")
    public ResponseEntity decreaseSubmitNumById(@PathVariable int id) {
        studentService.decreaseSubmitNumById(id);
        return new ResponseEntity(200, "ok", "ok");
    }


    @GetMapping("/getAllStudentsByKlassId1/{klassId}")
    @ApiOperation(value = "根据班级id查询该班级所有学生的所有信息")
    public ResponseEntity getAllStudentsByKlassId1(@PathVariable int klassId) {
        List<Student> students = studentService.getAllStudentsByKlassId1(klassId);
        return new ResponseEntity(200, "ok", students);
    }


    @GetMapping("/getSchoolBySchoolId/{schoolId}")
    @ApiOperation(value = "根据班级id查询该班级所有学生的所有信息")
    public ResponseEntity getSchoolBySchoolId(@PathVariable int schoolId) {
        School school = studentService.getSchoolBySchoolId(schoolId);
        return new ResponseEntity(200, "ok", school);
    }

    @PostMapping("/getByCondition")
    @ApiOperation(value = "多条件查询学生")
    public ResponseEntity getByCondition(@RequestBody Student student){
        List<Student> byCondition = studentService.getByCondition(student);
        return new ResponseEntity(200,"ok",byCondition);
    }
}
